package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.applet.domain.Wallet;
import com.ruoyi.project.applet.service.IWalletService;
import com.ruoyi.project.system.domain.SysUserWx;
import com.ruoyi.project.system.service.ISysUserWxService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2023-06-07
 */
@RestController
@RequestMapping("/user/wx")
public class  SysUserWxController extends BaseController
{
    @Autowired
    private ISysUserWxService sysUserWxService;

    @Autowired
    private IWalletService walletService;

    /**
     * 查询用户信息列表
     */
    @ApiOperation("查询用户信息列表")
    @GetMapping("/list")
    public TableDataInfo list(SysUserWx sysUserWx)
    {
        startPage();
        sysUserWx.setDelFlag("0");
        List<SysUserWx> list = sysUserWxService.selectSysUserWxList(sysUserWx);
        List<String> userIds = list.stream().map(SysUserWx::getOpenId).collect(Collectors.toList());
        List<Wallet> walletList = new ArrayList<>();
        for (String userId : userIds) {
            walletList.add(walletService.getWalletMoney(userId));
        }
        Map<String, Wallet> walletMap = walletList.stream().collect(Collectors.toMap(Wallet::getUserId, wallet -> wallet));
        list.stream().forEach(sysUserWx1 -> {
            String openId = sysUserWx1.getOpenId();
            Wallet wallet = walletMap.get(openId);
            sysUserWx1.setWallet(wallet);
        });

        return getDataTable(list);
    }

    @GetMapping("/list-all")
    public AjaxResult listAll()
    {
        SysUserWx sysUserWx = new SysUserWx();
        sysUserWx.setDelFlag("0");
        List<SysUserWx> list = sysUserWxService.selectSysUserWxList(sysUserWx);
        return AjaxResult.success(list);
    }



    /**
     * 导出用户信息列表
     */
    @ApiOperation("导出用户信息列表")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysUserWx sysUserWx)
    {
        List<SysUserWx> list = sysUserWxService.selectSysUserWxList(sysUserWx);

        List<String> userIds = list.stream().map(SysUserWx::getOpenId).collect(Collectors.toList());
        List<Wallet> walletList = new ArrayList<>();
        for (String userId : userIds) {
            walletList.add(walletService.getWalletMoney(userId));
        }

        Map<String, Wallet> walletMap = walletList.stream().collect(Collectors.toMap(Wallet::getUserId, wallet -> wallet));
        list.stream().forEach(sysUserWx1 -> {
            String openId = sysUserWx1.getOpenId();
            Wallet wallet = walletMap.get(openId);
            sysUserWx1.setWallet(wallet);
        });

        ExcelUtil<SysUserWx> util = new ExcelUtil<SysUserWx>(SysUserWx.class);
        return util.exportExcel(list, "wx");
    }

    /**
     * 获取用户信息详细信息
     */
    @ApiOperation("获取用户信息详细信息")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(sysUserWxService.selectSysUserWxById(userId));
    }


    /**
     * 新增用户信息
     */
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserWx sysUserWx)
    {
        String phonenumber = sysUserWx.getPhonenumber();
        Assert.hasText(phonenumber,"手机号不能为空");
        SysUserWx userWxByPhone = sysUserWxService.selectSysUserWxByPhone(phonenumber);
        Assert.isNull(userWxByPhone,"当前手机号已被绑定");
        sysUserWx.setPassword(SecurityUtils.encryptPassword(sysUserWx.getPassword()));
        return toAjax(sysUserWxService.insertSysUserWx(sysUserWx));
    }

    /**
     * 修改用户信息
     */
    @ApiOperation("修改用户信息")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserWx sysUserWx)
    {
        return toAjax(sysUserWxService.updateSysUserWx(sysUserWx));
    }

    /**
     * 删除用户信息
     */
    @ApiOperation("删除用户信息")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userId}")
    public AjaxResult remove(@PathVariable Long userId)
    {
        return toAjax(sysUserWxService.deleteSysUserWxById(userId));
    }

    /**
     * 小程序用户获取登录信息
     */
    @GetMapping("/token")
    public AjaxResult getInfoByToken()
    {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        if (userWx == null) {
            return AjaxResult.error(401,"请先登录");
        }
        Long userId =userWx.getUserId();
        return AjaxResult.success(sysUserWxService.selectSysUserWxByToken(userId));
    }
}
